package org.adream.account.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import org.aspectj.lang.JoinPoint;

public class ActionLog {
	Logger logger = LoggerFactory.getLogger(this.getClass());

	public void before(JoinPoint joinPoint) {
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		logger.debug("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
		logger.info("====ACTION " + methodName + "()" + " START===");
		logger.info(targetName + "::" + methodName);
		if(arguments.length>Constant.ZERO){
			logger.info("service params:");
			for(Object o :arguments){
				logger.info(String.valueOf(o));
			}
		}
	}

	public void after(JoinPoint joinPoint, Object retValue) {
		String methodName = joinPoint.getSignature().getName();
		if(retValue!=null){
			if(retValue instanceof Map){
				logger.info("service result:"+ retValue);
			}else if(retValue instanceof String){
				logger.info("service result:"+String.valueOf(retValue));
			}
		}
		
		logger.info("====ACTION " + methodName + "()" + " END====;");
	}
	
	public void afterThrowing(JoinPoint joinPoint, Throwable e) {
		logger.info(e.getMessage(),e);
		String methodName = joinPoint.getSignature().getName();
		logger.info("====ACTION " + methodName + "()" + " ERROR====;");
	}
	
}
